package com.xuecheng.manage_cms_client.mq;

import com.alibaba.fastjson.JSONObject;
import com.xuecheng.framework.domain.cms.CmsPage;
import com.xuecheng.manage_cms_client.service.impl.CmsPageServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;

/**
 * 监听MQ接收页面发布的消息
 */
@Component
public class ConsumerPostPage {
    private static final Logger LOGGER = LoggerFactory.getLogger(ConsumerPostPage.class);

    @Resource
    CmsPageServiceImpl cmsPageService;

    @RabbitListener(queues = {"${xuecheng.mq.queue}"})
    public void postPage(String message) {
        // json转换成map
        Map map = JSONObject.parseObject(message, Map.class);
        // 得到pageId
        String pageId = (String) map.get("pageId");
        // 校验
        CmsPage cmsPage = cmsPageService.findCmsPageById(pageId);
        if (cmsPage == null) {
            LOGGER.error("PostPage接收到的PageID不存在:" +pageId);
            return;
        }
        // 调用Service方法讲页面从GridFs下载到服务器
        cmsPageService.savePageToServerPath(pageId);
    }
}
